﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace server
{
    public partial class skill_manage : Form
    {
        Dictionary<string, Maticsoft.Model.Skill> skills = new Dictionary<string, Maticsoft.Model.Skill>();

        Maticsoft.Model.Skill skill1 = new Maticsoft.Model.Skill();
        public skill_manage()
        {
            InitializeComponent();
            loadSkill();
            foreach (Maticsoft.Model.Skill askill in skills.Values)
            {
                this.listSkill.Items.Add(askill.name);
            }
            this.btnDel.Enabled = false;
            this.btnMod.Enabled = false;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string name = this.txtname.Text;
            string desc = this.txtdesc.Text;
            int mp = int.Parse(this.txtmp.Text);
            int damage = int.Parse(this.txtdamage.Text);
            int damage_hot = int.Parse(this.txtdamage_hot.Text);
            int damage_hot_time = int.Parse(this.txtdamage_hot_time.Text);
            int damage_poison = int.Parse(this.txtdamage_poison.Text);
            int curse_at = int.Parse(this.txtcurse_at.Text);
            int hp_suck = int.Parse(this.txthp_suck.Text);
            int mp_suck = int.Parse(this.txtmp_suck.Text);
            int curse_de = int.Parse(this.txtcurse_de.Text);
            int needPoint = int.Parse(this.txtneedPoint.Text);
            string kind = this.comboBoxKind.Text;

            Maticsoft.Model.Skill model = new Maticsoft.Model.Skill();
            model.name = name;
            model.desc = desc;
            model.mp = mp;
            model.damage = damage;
            model.damage_hot = damage_hot;
            model.damage_hot_time = damage_hot_time;
            model.damage_poison = damage_poison;
            model.curse_at = curse_at;
            model.hp_suck = hp_suck;
            model.mp_suck = mp_suck;
            model.curse_de = curse_de;
            model.needPoint = needPoint;
            model.kind = kind;

            Maticsoft.DAL.Skill bll = new Maticsoft.DAL.Skill();
            bll.Add(model);
        }

        void loadSkill()
        {
            Maticsoft.DAL.Skill bll = new Maticsoft.DAL.Skill();
            DataSet ds = bll.GetList("");
            foreach(DataTable dt in ds.Tables)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Maticsoft.Model.Skill model = bll.DataRowToModel(dr);
                    skills.Add(model.name,model);
                }
            }
        }

        private void btnMod_Click(object sender, EventArgs e)
        {
            string name = this.txtname.Text;
            string desc = this.txtdesc.Text;
            int mp = int.Parse(this.txtmp.Text);
            int damage = int.Parse(this.txtdamage.Text);
            int damage_hot = int.Parse(this.txtdamage_hot.Text);
            int damage_hot_time = int.Parse(this.txtdamage_hot_time.Text);
            int damage_poison = int.Parse(this.txtdamage_poison.Text);
            int curse_at = int.Parse(this.txtcurse_at.Text);
            int hp_suck = int.Parse(this.txthp_suck.Text);
            int mp_suck = int.Parse(this.txtmp_suck.Text);
            int curse_de = int.Parse(this.txtcurse_de.Text);
            int needPoint = int.Parse(this.txtneedPoint.Text);
            string kind = this.comboBoxKind.Text;

            Maticsoft.Model.Skill model = new Maticsoft.Model.Skill();
            model.name = name;
            model.desc = desc;
            model.mp = mp;
            model.damage = damage;
            model.damage_hot = damage_hot;
            model.damage_hot_time = damage_hot_time;
            model.damage_poison = damage_poison;
            model.curse_at = curse_at;
            model.hp_suck = hp_suck;
            model.mp_suck = mp_suck;
            model.curse_de = curse_de;
            model.needPoint = needPoint;
            model.kind = kind;

            Maticsoft.DAL.Skill bll = new Maticsoft.DAL.Skill();
            bll.Update(model);
        }

        private void listSkill_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectName;
            selectName = this.listSkill.SelectedItem.ToString();
            this.skill1 = this.skills[selectName];


            this.txtname.Text = this.skill1.name;
            this.txtdesc.Text =skill1.desc;
            this.txtmp.Text = skill1.mp.ToString();
            this.txtdamage.Text =skill1.damage.ToString();
            this.txtdamage_hot.Text = skill1.damage_hot.ToString();
            this.txtdamage_hot_time.Text = skill1.damage_hot_time.ToString();
            this.txtdamage_poison.Text = skill1.damage_poison.ToString();
            this.txtcurse_at.Text = skill1.curse_at.ToString();
            this.txthp_suck.Text = skill1.hp_suck.ToString();
            this.txtmp_suck.Text = skill1.mp_suck.ToString();
            this.txtcurse_de.Text = skill1.curse_de.ToString();
            this.txtneedPoint.Text = skill1.needPoint.ToString();
            this.comboBoxKind.Text = skill1.kind;

            this.btnDel.Enabled = true;
            this.btnMod.Enabled = true;
            this.txtname.Enabled = false;
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            Maticsoft.DAL.Skill bll = new Maticsoft.DAL.Skill();
            bll.Delete(skill1.name);
        }

    }
}
